package cooperated

import (
	"fmt"

	"gddgame.cc/galaxy/common/namespaces/cooperate"
	"gddgame.cc/galaxy/common/namespaces/cooperate/structure"
	"gddgame.cc/galaxy/utils/serialize"
)

func (logic *TypeMap) Repair(slot *serialize.Mps, info *structure.ScheduleInfo, content *cooperate.TouchScheduleContent, repair *RepairLogic) {
	direct := logic.Center.DirectMap[repair.DirectId]

	instance := logic.GetDetailCache(logic, info.Target)
	//result := logic.GetPropsCache(conn, info.MapNoId, info.Target, []string{Position, Identity, Speed, MoveLockTime, AttackDistance, AttackSpeed, Health, Mode, Visible, Combat, Ignore, CrossedTime, BeCrossTime, BeCrossStartTime, CrossedItem, CrossedStartTime, BeCrossItem, Defend})
	if instance == nil {
		fmt.Println("repair props miss", info.Target)
		return
	}
	if direct.CancelDestroy && instance.ToBool(Ignore) {
		return
	}
	position := instance.ToBytes(Position)
	target := logic.GetDetailCache(logic, &repair.Target)
	if target == nil {
		fmt.Println("repair target props miss", repair.Target)
		return
	}
	if target.ToBool(Ignore) {
		return
	}

	if err := logic.attackByDirect(direct, instance, target); err != nil {
		return
	}
	nextTime := info.Time + int64(direct.IntervalTime)
	if nextTime > repair.StartTime+int64(direct.SustainedTime) {
		return
	}
	_ = logic.AddTouchSchedule(logic.GetMapNoId(), &cooperate.ScheduleContent{From: nil, Target: info.Target, Key: info.Key, Time: nextTime}, 1, position, direct.ScheduleType, repair, nil)

}
